<!DOCTYPE html>
<html lang="en">

    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>注册</title>
        <style>
            * {
                box-sizing: border-box;
            }

            html,
            body {
                width: 100%;
                height: 100%;
            }

            body {
                margin: 0;
                overflow: hidden;
                background-image: url(img/timg.jpg);
                background-size: 100% 105%;
            }

            #container {
                padding: 100px 30px;
            }

            #title {
                height: 50px;
                background-image: url("./images/Title.png");
                background-size: 100% 100%;
                line-height: 45px;
                text-align: center;
                color: black;
                font-size: 25px;
                font-weight: bold;
                margin-bottom: 50px;
            }

            .input-box {
                margin: 20px 0;
                text-align: center;
            }

            .input-box input {
                height: 35px;
                width: 200px;
                background-image: url(img/timg.jpg);
                background-size: 100% 100%;
                padding: 0 10px;
                font-size: 18px;
            }

            #go-register {
                text-align: right;
            }

            #go-register a {
                font-size: 12px;
                text-decoration: none;
                color: black;
            }

            #register {
                text-align: center;
            }

            #register button {
                width: 200px;
                height: 60px;
                background-image: url("./images/ButtonBlue.png");
                background-size: 100% 100%;
                border: none;
                background-color: transparent;
                color: black;
                font-size: 20px;
                margin-top: 100px;
            }
        </style>
    </head>

    <body>
        <div id="container">
            <!-- 标题 -->
            <div id="title">注册</div>
            <!-- 输入 -->
            <div>
                <div class="input-box">
                    <label for="username">账&#x3000;&#x3000;号：</label>
                    <input type="text" id="username" autocomplete="off">
                </div>
                <div class="input-box">
                    <label for="password">密&#x3000;&#x3000;码：</label>
                    <input type="password" id="password" autocomplete="off">
                </div>
                <div class="input-box">
                    <label for="repassword">确认密码：</label>
                    <input type="password" id="repassword" autocomplete="off">
                </div>
            </div>
            <!-- 登陆 -->
            <div id="register">
                <button>点击注册</button>
            </div>
        </div>
    </body>
    <script>
        // 点击注册按钮
        document.querySelector("#register button").onclick = function(){
            // 用户名
            var username = document.getElementById("username").value;
            // 密码
            var password = document.getElementById("password").value;
            // 确认密码
            var repassword = document.getElementById("repassword").value;
            // username.trim() 去掉两端的空格和换行
            if (!username.trim() || !password.trim() || !repassword.trim()) {
                alert("用户输入不能为空");
                return;
            }
            // 判断密码和确认密码是否一样
            if (password != repassword) {
                alert("两次输入的密码不一致");
                return;
            }
            // 生成账号小对象
            var account = {
                username: username,
                password: password
            }
            // 从本地拿之前注册过的账号数组
            var accounts = JSON.parse(localStorage.getItem("accounts"));
            if (!accounts) {
                accounts = [];
            }
            // 判断当前这个账号有没有被抢注
            var exist = accounts.some(function(ele){
                return ele.username == account.username;
            });
            if (!exist) {
                // 把新账号存入数组
                accounts.push(account);
            }else{
                alert("此账号已被注册!");
                return;
            }
            // 把账号数组再存入本地（覆盖）
            localStorage.setItem("accounts", JSON.stringify(accounts));
            // 弹出注册成功的提示框
            var con = confirm("注册成功，是否跳转到登陆页面？");
            if (con) {
                // 跳转登陆
                location.href = "节奏大师.html";
            } 
        }
    </script>
</html>